package so.ifl.commons.tools;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import so.ifl.vo.CompanyVo;

/**
 * 
 * @author simon
 * @created in 2012-12-21 - 下午05:58:50 本类说明：对象转Map
 */
public class ObjAnalysis {

	public static Map ConvertObjToMap(Object obj) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		if (obj == null)
			return null;
		Field[] fields = obj.getClass().getDeclaredFields();
		try {
			for (int i = 0; i < fields.length; i++) {
				try {
					Field f = obj.getClass().getDeclaredField(
							fields[i].getName());
					f.setAccessible(true);
					Object o = f.get(obj);
					reMap.put(fields[i].getName(), o);
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return reMap;
	}

	public static void main(String[] args) {
		CompanyVo a = new CompanyVo();
		a.setAllowStudent(1);
		a.setCompanyAbridge("sdfsf");
		a.toString();
		Map m = ConvertObjToMap(a);
		System.out.println(a);
	}

}
